#include<stdio.h>
#include<unistd.h>
#include <pthread.h>
/*
 *
This is main thread, pid=32461, tid=140434886096448
This is thread test, pid=32461, tid=140434886096448
This is 0 thread.
This is main thread, pid=32461, tid=140434877703744
This is thread test, pid=32461, tid=140434877703744
This is 1 thread.
This is main thread, pid=32461, tid=140434869311040
This is main thread, pid=32461, tid=140434860918336
This is main thread, pid=32461, tid=140434852525632
This is thread test, pid=32461, tid=140434860918336
This is 3 thread.
This is thread test, pid=32461, tid=140434869311040
This is 2 thread.
This is thread test, pid=32461, tid=140434852525632
This is 4 thread.

 */

void * testThread(void * arg){
	printf("This is thread test, pid=%d, tid=%lu\n", getpid(), pthread_self());
	printf("This is %d thread.\n", (int)arg);
	pthread_exit(NULL);
	//线程已经结束，这一行就不会打印了
	printf("after pthread exit\n");
}

int main(){
	pthread_t tid[5];
	int ret;
	int arg = 5;
	int i;
	for(i = 0; i < 5; i++){
		//最后一个参数，使用值传递，尽管编译报错，但是会达到预期的打印值0、1、2、3、4。执行的成功的原因：地址也是整数，可以认为是0、1、2、3、4的地址
		//如果使用地址传递，那么会由于主程序执行太快，导致子线程取地址的值都是5，打印出来的就是5、5、5、5、5
		ret = pthread_create(&tid[i], NULL, testThread, (void *)i);
		printf("This is main thread, pid=%d, tid=%lu\n", getpid(), tid[i]);
	}
	//主进程需要停留1秒，不然线程内容都无法执行
	//主进程一旦执行完，线程都将不存在
	sleep(1);
}
